home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu745.dms / pu745.adf / GLOBE099.LHA / Ami-Globe / divers.c < prev    next >
C/C++ Source or Header  |  1994-11-05  |  13KB  |  405 lines

  1. /************************************************************************/
  2. /*                                                                      */
  3. /*      fichier         : divers.c                                      */
  4. /*      projet          : amiglobe                                      */
  5. /*      date création   : juin 1994                                     */
  6. /*      commentaire     : fonctions diverses:                           */
  7. /*                        recherche de capitale                         */
  8. /*                        fermeture de fenêtres                         */
  9. /*                        chargement/sauvegarde des preferences         */
  10. /*                        calcul de la distance                         */
  11. /*      révision        : $VER: divers.c 1.003 (29 Sep 1994) Sep 1994) */
  12. /*      copyright       : Olivier Collard, Thomas Landspurg             */
  13. /*                                                                      */
  14. /************************************************************************/
  15.  
  16.  
  17. /************************************************************************/
  18. /*      includes                                                        */
  19. /************************************************************************/
  20. #include <clib/exec_protos.h>
  21. #include <proto/muimaster.h>
  22. #include <exec/types.h>
  23. #include <exec/nodes.h>
  24. #include <exec/lists.h>
  25. #include <exec/ports.h>
  26. #include <graphics/gfxbase.h>
  27. #include <clib/intuition_protos.h>
  28. #include <clib/macros.h>
  29. #include <math.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <libraries/mui.h>
  34.  
  35. #include "amiglobe_interf.h"
  36. #include "amiglobe_types.h"
  37. #include "divers.h"
  38. #include "chemin.h"
  39. /* Prototype get_pays_name */
  40. char * get_pays_name (int);
  41.  
  42. /************************************************************************/
  43. /*      defines                                                        */
  44. /************************************************************************/
  45.  
  46. #define MAX_ELEM_REM 1500
  47.  
  48. /************************************************************************/
  49. /*      prototypes                                                      */
  50. /************************************************************************/
  51.  
  52. float   Map_Convert_Angle(int angle);
  53.  
  54. /************************************************************************/
  55. /*      variables externes                                              */
  56. /************************************************************************/
  57.  
  58. extern struct  ObjApp  *My_App; 
  59. extern COUNTRY * tab_country;
  60. ELEM_REM ** tab_elem;
  61. extern int max_elem;
  62. extern int max_country;
  63. extern TRAJECTOIRE ** tab_trajectoire;
  64. extern CLIP clip_max;
  65. extern PREFERENCE Pref;
  66. extern float distance_orig_dest;
  67. extern struct Window * wG;
  68. extern struct GfxBase * GfxBase;
  69.  
  70. /************************************************************************/
  71. /*      variables privees                                               */
  72. /************************************************************************/
  73.  
  74. PREFERENCE Default_Pref=
  75. ///
  76. {
  77.     0,0,0,0,0,0,0,
  78.     PROJ_FLAT,
  79.     TRACE_FIL,
  80.     0,0, /*glob_min*/
  81.     0,0,0,
  82.     HIRES_KEY,640,256,4,OSCAN_STANDARD,
  83.     0,0,0,
  84.     0,0,
  85.     {0,0,FALSE},{0,0,FALSE},
  86.     -1,
  87.     640,256,
  88.     5,0
  89. };      
  90. ///
  91.  
  92. /************************************************************************/
  93. /*      implémentation                                              */
  94. /************************************************************************/
  95.  
  96. void CloseWindowSafely(struct Window * win)
  97. ///
  98. {
  99.     struct IntuiMessage * msg;
  100.     struct Node * succ;
  101.     msg=(struct IntuiMessage *)(win->UserPort)->mp_MsgList.lh_Head;
  102.     while (succ = msg->ExecMessage.mn_Node.ln_Succ)
  103.     {
  104.         if (msg->IDCMPWindow == win)
  105.         {
  106.             Remove((struct Node *)msg);
  107.             ReplyMsg((struct Message *)msg);
  108.         }
  109.         msg=(struct IntuiMessage *)succ;
  110.     }
  111.     win->UserPort=NULL;
  112.     ModifyIDCMP(win,0L);
  113.     CloseWindow(win);
  114. }       
  115. ///
  116.  
  117. float distance(int x1,int y1,int x2,int y2)
  118. ///
  119. {
  120.     float X1=Map_Convert_Angle(x1);
  121.     float X2=Map_Convert_Angle(x2);
  122.     float Y1=Map_Convert_Angle(y1);
  123.     float Y2=Map_Convert_Angle(y2); 
  124.     float angle=acos(sin(Y1)*sin(Y2)+cos(Y1)*cos(Y2)*cos(X1-X2));
  125.     
  126.     return (float)(angle*RAYON);
  127. }
  128. ///
  129.  
  130. void create_tab_elem(void)
  131. ///
  132. {
  133.     int i;
  134.     int j=0;
  135.     ELEM_REM * element;
  136.     tab_elem=(ELEM_REM **)malloc(sizeof(ELEM_REM *)*MAX_ELEM_REM);
  137.     for (i=0;i<max_country;i++)
  138.     {
  139.         element=tab_country[i].P_Elem_Rem;
  140.         while (element!=NULL)
  141.         {
  142.             tab_elem[j]=element;
  143.             element=element->P_Next;
  144.             j++;
  145.         }
  146.     }
  147.     printf("nbr d'élements:%d\n",j);
  148.     max_elem=j;
  149.     tab_elem[j]=NULL;
  150. }
  151. ///
  152.  
  153. ELEM_REM * capitale(int num_pays)
  154. ///
  155. {
  156.     ELEM_REM *element=tab_country[num_pays].P_Elem_Rem;
  157.     while (element!=NULL)
  158.     {
  159.         if (element->Type==CAPITALE)
  160.         {
  161.             return element;
  162.         }
  163.         element=element->P_Next;
  164.     }
  165.     return NULL;
  166. }
  167. ///
  168.  
  169. int pays(ELEM_REM * element)
  170. /*rend le numero du pays contenant l'element*/
  171. ///
  172. {
  173.         int i;
  174.         ELEM_REM * auxElement;
  175.         for (i=0;i<max_country;i++)
  176.         {
  177.                 auxElement=tab_country[i].P_Elem_Rem;
  178.                 while (auxElement!=NULL)
  179.                 {
  180.                         if (auxElement==element)
  181.                                 return i;
  182.                         auxElement=auxElement->P_Next;
  183.                 }
  184.         }
  185.         return -1;
  186. }       
  187. ///
  188.  
  189. void delete_element(ELEM_REM *element)
  190. /* efface recursivement tous les elements suivants et lui-même*/
  191. ///
  192. {
  193.         if (element!=NULL)
  194.         {
  195.                 delete_element(element->P_Next);
  196.                 free(element);
  197.         }
  198. }       
  199. ///
  200.  
  201. /* lit des trajectoires dans un fichier */
  202. void read_trajectoires(void)
  203. ///
  204. {
  205.     char * P_Name;
  206.     FILE * Fichier_Trajectoire;
  207.     int num_vect=0;
  208.     int num_traj=0;
  209.     int i;
  210.     get(My_App->STR_Trajectoire_Fichier,MUIA_String_Contents,(APTR)&P_Name);
  211.     Fichier_Trajectoire=fopen(P_Name,"r");
  212.     if (Fichier_Trajectoire!=NULL)
  213.     {
  214.         while (tab_trajectoire[num_traj]!=NULL)
  215.                 num_traj++;
  216.         while (1)
  217.         {
  218.             TRAJECTOIRE * trajectoire;
  219.             char ligne[256];
  220.             char * end=ligne;
  221.             short int * tab_vect=(short int *)malloc(sizeof(short int)*400);
  222.             short int * tab_vect2;
  223.             sprintf(ligne,"");
  224.             /* recherche du nom de la trajectoire*/
  225.             while ( (end!=NULL) && (strlen(ligne)<4) )
  226.             {
  227.                 printf("cherche nom\n");
  228.                 end=fgets(ligne,256,Fichier_Trajectoire);
  229.             }
  230.             if (end==NULL)
  231.                 break;
  232.             printf("longueur ligne:%d\n",strlen(ligne));
  233.             printf("trouvé:%s\n",ligne);
  234.             trajectoire=(TRAJECTOIRE *)malloc(sizeof(TRAJECTOIRE));
  235.             trajectoire->nom=strdup(ligne);
  236.             do
  237.             {
  238.                 printf("cherche début donnees\n");
  239.                 end=fgets(ligne,256,Fichier_Trajectoire);
  240.             }
  241.               while ( (end!=NULL) && (strlen(ligne)<4) );
  242.             if (end==NULL)
  243.                 break;
  244.             trajectoire->clip.maxx=-32000;
  245.             trajectoire->clip.minx=32000;
  246.             trajectoire->clip.maxy=-32000;
  247.             trajectoire->clip.miny=32000;
  248.             while ( (end!=NULL) && (strlen(ligne)>4) )
  249.             {
  250.                 char * lon,*lat,*code;
  251.                 lat=strtok(ligne,"$");
  252.                 lon=strtok(NULL,"$");
  253.                 code=strtok(NULL,"$");
  254.                 if ((lat!=NULL)&&(lon!=NULL))
  255.                 {
  256.                     if ( (code==NULL) || (atoi(code)<1) || (atoi(code)>5) )
  257.                     {
  258.                         tab_vect[num_vect*3]=(short int)5;
  259.                     }
  260.                     else
  261.                         tab_vect[num_vect*3]=(short int)atoi(code);
  262.                     tab_vect[num_vect*3+1]=(short int)atoi(lon);
  263.                     if (atoi(lon)>trajectoire->clip.maxx)
  264.                             trajectoire->clip.maxx=atoi(lon);
  265.                     if (atoi(lon)<trajectoire->clip.minx)
  266.                             trajectoire->clip.minx=atoi(lon);
  267.                     if (-atoi(lat)>trajectoire->clip.maxy)
  268.                             trajectoire->clip.maxy=-atoi(lat);
  269.                     if (-atoi(lat)<trajectoire->clip.miny)
  270.                             trajectoire->clip.miny=-atoi(lat);
  271.                                     
  272.                     tab_vect[num_vect*3+2]=(short int)(-atoi(lat));
  273.                     num_vect++;
  274.                 }
  275.                 printf("lecture d'un vecteur lat=%d  lon=%d\n",
  276.                                 atoi(lat),atoi(lon));
  277.                 end=fgets(ligne,256,Fichier_Trajectoire);
  278.             }
  279.             /* le tableau des vecteurs est prêt*/
  280.             tab_vect2=(short int *)malloc(3*sizeof(short int)*num_vect);
  281.             for (i=0;i<num_vect*3;i++)
  282.                     tab_vect2[i]=tab_vect[i];
  283.             trajectoire->vecteurs=(VECT*)tab_vect2;
  284.             trajectoire->nb_vect=num_vect;
  285.             /* rangement dans le tableau */
  286.             tab_trajectoire[num_traj]=trajectoire;
  287.             num_traj++;
  288.             tab_trajectoire[num_traj]=NULL;
  289.             /* initialisation des variables*/
  290.             num_vect=0;
  291.             free(tab_vect);         
  292.         }
  293.     }
  294.     printf("fichier non trouvé\n");
  295. }
  296. ///
  297.  
  298. void init_preference(void)
  299. ///
  300. {
  301.         Pref=Default_Pref;
  302.         Pref.clip_cur=clip_max;
  303. }
  304. ///
  305.  
  306. BOOL load_preference(void)
  307. ///
  308. {
  309.         FILE * fichier;
  310.         
  311.         fichier=fopen("amiglobe.prefs","r");
  312.         if (fichier==NULL)
  313.         {
  314.                 printf("impossible de trouver le fichier preference\n");
  315.                 Pref.clip_cur.minx=-18000;
  316.                 Pref.clip_cur.miny=-9000;
  317.                 Pref.clip_cur.maxx=18000;
  318.                 Pref.clip_cur.maxy=9000;
  319.                 return FALSE;
  320.         }
  321.         else
  322.         {
  323.                 int sel=0;
  324.                 fread(&Pref,sizeof(PREFERENCE),1,fichier);
  325.                 fclose(fichier);
  326.                 set(My_App->CH_Lacs,MUIA_Selected,Pref.Flg_Draw_Lac);
  327.                 set(My_App->CH_Riv,MUIA_Selected,Pref.Flg_Draw_Riv);
  328.                 set(My_App->CH_Coord,MUIA_Selected,Pref.Flg_Aff_Coord);
  329.                 set(My_App->CH_Noms,MUIA_Selected,Pref.Flg_Aff_Country_Name);
  330. /*              set(My_App->CH_Fill_Pays,MUIA_Selected,Pref.Flg_Fill_Pays);*/
  331.                 set(My_App->CH_Elements,MUIA_Selected,Pref.Flg_Aff_Elem_Rem);
  332.                 set(My_App->CH_LonLat,MUIA_Selected,Pref.Flg_Aff_LatLon);
  333.                 set(My_App->CH_Distance,MUIA_Selected,Pref.Flag_Distance);
  334.                 set(My_App->CH_Area,MUIA_Selected,Pref.Flag_Surface);
  335.                 set(My_App->CH_Scale,MUIA_Selected,Pref.Flag_Echelle);
  336.                 set(My_App->CH_Trajectoires,MUIA_Selected,Pref.Flg_Aff_Trajectoires);
  337.                 set(My_App->SL_x,MUIA_Slider_Level,Pref.SL_x);
  338.                 set(My_App->SL_y,MUIA_Slider_Level,Pref.SL_y);
  339.                 set(My_App->SL_z,MUIA_Slider_Level,Pref.SL_z);
  340.                switch(Pref.Type_Proj)
  341.                 {
  342.                         case PROJ_FLAT:
  343.                                 if (Pref.Flg_Proj_3D==TRUE)
  344.                                         sel=3;
  345.                                 else
  346.                                         sel=0;
  347.                                 break;
  348.                         case PROJ_MERCATOR:
  349.                                 sel=1;
  350.                                 break;
  351.                         case PROJ_GLOBE:
  352.                                 sel=2;
  353.                                 break;
  354.                         default:
  355.                                 sel=0;
  356.                 }
  357.                 set (My_App->CY_Proj,MUIA_Cycle_Active,sel);
  358.  
  359.                 Update_Points();
  360.                 return TRUE;
  361.         }
  362. }
  363. ///
  364.  
  365. BOOL save_preference(void)
  366. ///
  367. {
  368. /*        FILE * fichier;
  369.         
  370.         fichier=fopen("amiglobe.prefs","w");
  371.         if (fichier==NULL)
  372.         {
  373.                 printf("impossible d'ouvrir le fichier preference");
  374.                 return FALSE;
  375.         }
  376.         else
  377.         {
  378.                 fwrite(&Pref,sizeof(PREFERENCE),1,fichier);
  379.                 fclose(fichier);
  380.                 return TRUE;
  381.         }*/
  382.         return TRUE;
  383. }               
  384. ///
  385.  
  386. void Application_Sleep(BOOL booleen)
  387. {
  388.     static int increment=0;
  389.     set(My_App->App,MUIA_Application_Sleep,booleen);
  390.     if (booleen==TRUE)
  391.     {
  392.         increment ++;
  393.         /* busy pointer*/
  394.         if (increment==1 && GfxBase->LibNode.lib_Version>=39)
  395.             SetWindowPointer(wG,
  396.                 WA_BusyPointer,TRUE,WA_PointerDelay,TRUE,TAG_DONE);
  397.     }
  398.     else
  399.     {
  400.         if (increment==1 && GfxBase->LibNode.lib_Version>=39)
  401.             SetWindowPointer(wG,TAG_DONE);
  402.         increment--;
  403.     }
  404. }
  405.